<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts 主体
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '250px'
    },
    btDat: {
      type: Array,
      default: () => {
        return []
      }
    },
    title: {
      type: String,
      default: '信息'
    }
  },
  watch: {
    btDat(name, value) {
      this.datas = name
      this.initChart()
    }
  },
  data() {
    return {
      chart: null,
      datas: []
    }
  },
  mounted() {
    this.$nextTick(() => {

    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      let username = this.datas.map(item => item.name)
      // console.log("饼图", this.datas, 'username', username)
      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption({
        //  backgroundColor: 'rgba(1,202,217,.2)',
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        // legend: {
        //   left: 'center',
        //   bottom: '0',
        //   data: username,
        //   textStyle: {
        //     color: 'rgba(255,255,255,.7)'
        //   },
        //   icon: "circle"
        // },
        color: ['#1fb5fc','#fc5659','#fd895b', '#236bd7','#fed130','#27e9cb','#a9a9a9','#e86ba2','#8bc899','#c391be'],
        series: [
          {
            name: this.title,
            type: 'pie',
            // radius: [15, 95],
            // center: ['50%', '38%'],
            // roseType: 'radius',
            radius: ['40%', '55%'],
            avoidLabelOverlap: false,
            // label: {
            //   show: false,
            //   position: 'center',
            //   formatter: ['{b}: ', '( {c} )'].join(''),
            //   color: '#fff'
            // },
            emphasis: {
              label: {
                show: true,
                fontSize: '12',
                fontWeight: 'bold'
              }
            },
            // labelLine: {
            //   show: false
            // },
            data: this.datas,
            animationEasing: 'cubicInOut',
            animationDuration: 2600
          }
        ]
      })
    }
  }
}
</script>
